﻿
Imports std

Namespace defaultValue
    Public Module _defaultValue
        Public Const storoom As String = "hzj-jie-8320-1:30002"
        Public Const jobinfoRootDirectory As String = "runrroom/jobinfo/"
        Public Const jobinfoHistoryStoroomKey As String = "runrroom/history/jobinfo"
        Public Const jobstartScript As String = "start.lpms"
        Public Const scriptEntryFunction As String = "main"
        Public ReadOnly localrunProcessOutputDirectory As String = _
            combinepath(filesystem.deployRoot, "localrunProcessOutput")
    End Module
End Namespace

Namespace protocols
    Public Module _protocols
        Public Const action As String = "act"
        Public Const jobid As String = "jid"
        Public Const command As String = "cmd"
        Public Const distrruleId As String = "dti"
        Public Const distrrule As String = "dtr"
        Public Const [step] As String = "stp"
        Public Const percentage As String = "pct"
        Public Const result As String = "rst"
        Public Const errorInfo As String = "eif"
    End Module

    Namespace results
        Public Module _results
            Public Const success As String = "suc"
            Public Const failed As String = "fal"
        End Module
    End Namespace

    Namespace actions
        Public Module _actions
            Public Const run As String = "run"
            Public Const status As String = "sts"
            Public Const updateStatus As String = "ust"
        End Module
    End Namespace

    Namespace run_info
        Public Module _run_info
        End Module
    End Namespace
End Namespace

Namespace environment_variables
    Public Module environment_variables
        Public Const localrunJobId As String = "RUNRROOM_LOCALRUN_JOBID"
    End Module
End Namespace

Namespace connection_const
    Namespace port
        Public Module _port
            Public Const localrun As Int32 = 20001
            Public Const distrrun As Int32 = 20002
        End Module
    End Namespace

    Namespace distrrun_localrun
        Public Module _distrrun_localrun
            Public Const holdtime As Int64 = (tcpServer.defaultHoldTime >> 1)
            Public Const livetime As Int64 = (tcpServer.defaultLiveTime << 4)
            'at most one talk for each thread
            Public ReadOnly connectionCount As Int64 = rrserver.defaultThreadCount
        End Module
    End Namespace

    Namespace delegate_localrun
        Public Module _delegate_localrun
            Public Const holdtime As Int64 = (tcpServer.defaultHoldTime >> 3)
            Public Const livetime As Int64 = (tcpServer.defaultLiveTime << 4)
        End Module
    End Namespace

    Namespace client_distrrun
        Public Module _client_distrrun
            Public Const holdtime As Int64 = (tcpServer.defaultHoldTime)
            Public Const livetime As Int64 = (tcpServer.defaultLiveTime >> 4)
        End Module
    End Namespace
End Namespace

Public Module _runrroom
    Public Const commandSeparator As Char = character.semicolon

    Public Function splitCommand(ByVal cmd As String, ByRef app As String, ByRef arg As String) As Boolean
        Dim vs As vector(Of String) = Nothing
        strsepar(cmd, commandSeparator, vs, True)
        Select Case vs.size()
            Case 1
                app = vs(0)
                arg = Nothing
            Case 2
                app = vs(0)
                arg = vs(1)
            Case Else
                Return False
        End Select

        Return True
    End Function

    Public Function assembleCommand(ByRef cmd As String, _
                                    ByVal app As String, Optional ByVal arg As String = Nothing) As Boolean
        If isemptystring(arg) Then
            copy(cmd, app)
        Else
            cmd = app + commandSeparator + arg
        End If
        Return True
    End Function

    Private Function match(ByVal s1 As String, ByVal s2 As String) As Boolean
        Return strsame(s1, s2, False)
    End Function

    Public Function protocol(ByVal p As pair(Of String, String)) As String
        assert(Not p Is Nothing)
        Return p.first
    End Function

    Public Function action(ByVal p As pair(Of String, String)) As String
        assert(Not p Is Nothing)
        Return p.second
    End Function

    Public Function result(ByVal p As pair(Of String, String)) As String
        Return action(p)
    End Function

    Public Function matchProtocol(ByVal p As pair(Of String, String), ByVal ptc As String) As Boolean
        Return match(protocol(p), ptc)
    End Function

    Public Function matchAction(ByVal p As pair(Of String, String), ByVal act As String) As Boolean
        Return match(action(p), act)
    End Function

    Public Function matchResult(ByVal p As pair(Of String, String), ByVal rst As String) As Boolean
        Return match(result(p), rst)
    End Function
End Module